package com.eeepay.eeepay_v2.activity;

import android.os.SystemClock;
import android.widget.ListView;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.adapter.SubsidyRecordAdapter;
import com.eeepay.eeepay_v2.api.SubsidiesDetailRecordAPI;
import com.eeepay.v2_library.pulltorefresh.PullToRefreshBase;
import com.eeepay.v2_library.pulltorefresh.PullToRefreshListView;
import com.eeepay.v2_library.ui.ABBaseActivity;

import java.util.List;

import cn.eeepay.api.grpc.nano.ManagementCenterProto;


/**
 * 描述：账号记录
 * 作者：zhuangzeqin
 * 时间: 2017/5/3-11:43
 * 邮箱：zzq@eeepay.cn
 */
public class SubsidyRecordActivity extends ABBaseActivity {
    private static final int REQUEST_RECORD_TASKID = 1000;//请求是任务id
    private PullToRefreshListView lv_record;//提现记录列表
    private SubsidyRecordAdapter recordAdapter;//适配器
    private int currPage = 1;//当前页
    private int mTotal = 0;//总的记录数
    private SubsidiesDetailRecordAPI subsidiesDetailRecordAPI;//获取数据的api请求

    @Override
    protected int getLayoutId() {
        return R.layout.activity_withdrawal_record;
    }

    @Override
    protected void initWidget() {
        lv_record = getViewById(R.id.lv_record);
        subsidiesDetailRecordAPI = new SubsidiesDetailRecordAPI();
        subsidiesDetailRecordAPI.setResultCallBack(resultCallBack);
        recordAdapter = new SubsidyRecordAdapter(mContext);
        lv_record.setAdapter(recordAdapter);
        sendHttpRequest(REQUEST_RECORD_TASKID);
    }

    /**
     * 数据的接口的监听回调
     */
    final SubsidiesDetailRecordAPI.ResultCallBack resultCallBack = new SubsidiesDetailRecordAPI.ResultCallBack() {
        @Override
        public void oninit() {
            showProgressDialog();
        }

        @Override
        public void onFail(String msg) {
            showToast(msg);
        }

        @Override
        public void onSucess(int currPage,int total,List<ManagementCenterProto.ActivitySubsidiesDetail> subsidiesDetails) {
            mTotal = total;//获取总的记录数
            if (currPage==1)
                recordAdapter.setList(subsidiesDetails);
            else
                recordAdapter.addAll(subsidiesDetails);
        }

        @Override
        public void onUnInit() {
            dismissProgressDialog();
            if (lv_record.isRefreshing()) {
                lv_record.onRefreshComplete();
            }
        }
    };

    @Override
    protected void eventOnClick() {
    lv_record.setMode(PullToRefreshBase.Mode.BOTH);
        lv_record.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            /** 往下拉取 获取最新数据 **/
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                currPage = 1;
                sendHttpRequest(REQUEST_RECORD_TASKID);
            }

            /** 往上加载 ，加载更多数据**/
            @Override
            public void onPullUpToRefresh(final PullToRefreshBase<ListView> refreshView) {
                if (mTotal > currPage * SubsidiesDetailRecordAPI.PAGESIZE) {//如果还小于总的大小；接着请求下10条记录
                    currPage++;
                    sendHttpRequest(REQUEST_RECORD_TASKID);
                } else {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            SystemClock.sleep(1000);
                            showToast("已经是最后一页了");
                            if (refreshView.isRefreshing()) {
                                refreshView.onRefreshComplete();
                            }
                        }
                    });
                }
            }
        });
    }

    /**
     * 开始网络请求
     *
     * @param taskId
     */
    @Override
    protected void sendHttpRequest(int taskId) {
        if (REQUEST_RECORD_TASKID==taskId)
            subsidiesDetailRecordAPI.getSubsidiesRecordInfo(currPage);
    }
}
